<?php

/**
 * Created by PhpStorm.
 * User: 张彪
 * Date: 2016/5/11 0011
 * Time: 下午 5:46
 */
class Track_model extends Base_Model
{
    public function __construct()
    {
        $this->load->database();
    }


    public function getUserInfoById($uid)
    {
        $key_info = array("telActive","identityErea_zhuanye", "identityErea_school","userIcon", "identityActive", "active", 'telCode', 'telNum', 'identityErea', 'identityName', 'identityPicture', 'email', 'failMsg', 'utype');
        $key_user_info = array('userAge', 'nationality', 'language', 'introduction', 'resideCity', 'resideTime', 'country', 'code', 'rcity', 'addr', 'userGender');


        $query = $this->db->query("select * from user_v2 as u1 LEFT JOIN user_v2_userinfo as u2 on u1.uid = u2.uid  WHERE u1.uid ={$uid}");
        $userInfo = $query->row_array();

        $baseInfo = array();
        foreach ($key_user_info as $v) {
            $baseInfo[$v] = empty($userInfo[$v]) ? '' : $userInfo[$v];
        }
        foreach ($key_info as $v) {
            $baseInfo[$v] = empty($userInfo[$v]) ? '' : $userInfo[$v];
        }

        if ($baseInfo['identityName'] == '留学生') {
            $data = explode('-|-', $baseInfo['identityErea']);
            $baseInfo['identityErea_zhuanye'] = empty($data[false]) ? '' : $data[false];
            $baseInfo['identityErea_school'] = empty($data[1]) ? '' : $data[1];
        }

        $lang = array();
        foreach (explode(',', $userInfo['language']) as $v) {
            if ($v == 1) {
                $lang[] = '中文';
            } elseif ($v == 2) {
                $lang[] = '法文';
            } elseif ($v == 3) {
                $lang[] = '英文';
            }
        }
        $baseInfo['language'] = $lang;

        $userFamily = empty($userInfo['userFamily']) ? '' : $userInfo['userFamily'];
        $userName = empty($userInfo['userName']) ? '' : $userInfo['userName'];
        $baseInfo['realName'] = $userFamily . $userName;

        return $baseInfo;
    }
    /**
     * @param $id
     * @return mixed
     * 获取单个详情
     */
    public function detail($id)
    {

        $this->db->where('id', $id);
        $this->db->from('mod_track');
        $query = $this->db->get()->row_array();

        if(!empty($query['uid']))
        {
            $guider = $this->getUserInfoById($query['uid']);
        }else{
            return false;
        }
        $Imgs = explode('|',$query['images']);
        if(!empty($Imgs))
        {
            foreach ($Imgs as $k=>$img)
            {
                $Img[$k] = $this->img($img);
            }

        }

        $item['name'] = $query['name'];
        $item['userIcon'] = $this->img($guider['userIcon']);
        $item['realName'] = $guider['realName'];
        $item['identityActive'] = $guider['identityActive'];
        $item['telActive'] = $guider['telActive'];
        $item['resideCity'] = $guider['resideCity'];
        $item['resideTime'] = $guider['resideTime'];
        $item['userAge'] = $guider['userAge'];
        $item['userGender'] = $guider['userGender'];
        $item['identityName'] = $guider['identityName'];
        $item['identityErea_zhuanye'] = $guider['identityErea_zhuanye'];
        $item['identityErea_school'] = $guider['identityErea_school'];
        $item['introduction'] = nl2br($guider['introduction']);
        $item['serviceMaxPersons'] = $query['serviceMaxPersons'];
        $item['summary'] = nl2br($query['summary']);

        $item['images'] = $Imgs;
//        $item['price'] = ($query['priceService']/100)."€";
//        $item['descService'] = nl2br($query['descService']);
//        $item['hours'] = $query['hours'];
//        $item['location'] = $query['location'];
//        $item['todolist'] = nl2br($query['todolist']);
        $item['priceService'] = $query['priceService'];
//        $item['descService'] = $query['descService'];
        $item['priceTicket'] = $query['priceTicket'];
        $item['descTicket'] = $query['descTicket'];
        $item['priceTraffic'] = $query['priceTraffic'];
        $item['descTraffic'] = $query['descTraffic'];
        $item['priceFood'] = $query['priceFood'];
        $item['descFood'] = $query['descFood'];
        $item['priceOther'] = $query['priceOther'];
        $item['descOther'] = $query['descOther'];
        $item['name'] = $query['name'];
        $item['todulist'] = $this->gettodolist($id);

        return $item;
//        var_dump($item);die;

    }

    public function gettodolist($id)
    {
        $this->db->select('t.dayNo,t.scenicId,t.timeStart,t.timeStop,t.desc,s.name,s.thumb');
        $this->db->where('trackId', $id);
        $this->db->from('mod_track_days as t');
        $this->db->join('mod_scenic_spot as s', 't.scenicId = s.id', 'left');
        $query = $this->db->get()->result_array();
        foreach ($query as &$q)
        {
            $q['thumb'] = $this->img($q['thumb']);
        }
        return $query;
    }

    public function getbyid($id)
    {
        $this->db->where('id', $id);
        $this->db->from('mod_track');
        $query = $this->db->get()->row_array();
        return $query;
    }

    
}